package config

import (
	"freechat-im/util"
	"github.com/samuel/go-zookeeper/zk"
	"github.com/sirupsen/logrus"
	"sync"
	"time"
)

var zkConn *zk.Conn
var once5 sync.Once // 确保初始化只执行一次的同步工具

func InitZookeeper(zkHost string) {
	once5.Do(func() {

		// 连接到ZooKeeper集群，可以指定多个地址以逗号分隔
		conn, _, err := zk.Connect([]string{zkHost}, time.Second) //*zk.Conn, *zk.Event, error
		if err != nil {
			logrus.Fatal("连接ZooKeeper失败 ", err)
		}
		zkConn = conn

		util.ZkUtil = &util.ZkUtils{
			Conn: conn,
		}
	})
}

func GetZkConn() *zk.Conn {
	return zkConn
}
